package serializables;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
 * @Description
 * @Author zhaobaolin
 * @Date 2018/11/12
 */
public class ObjectStreamExample {
    public static void main(String[] args) {
        demo();
    }

    private static void demo()
    {
        try(FileOutputStream file = new FileOutputStream("./io/src/serializables/1.txt")){
            ObjectOutputStream oos = new ObjectOutputStream(file);
            oos.writeInt(12345);
            oos.writeObject("Today2");
            oos.writeObject(new Date());
            oos.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        try(FileInputStream file = new FileInputStream("./io/src/serializables/1.txt")){
            ObjectInputStream ois = new ObjectInputStream(file);
            int i = ois.readInt();
            String name = (String) ois.readObject();
            Date date = (Date) ois.readObject();
            ois.close();
            System.out.println("i:"+i+",name:"+name+",date:"+date);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
